package org.springboot.szsrcb.util;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlOperateUtil {
	public static void update_req_type_in_req_new(){
		String xmlPath="";
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		dbf.setIgnoringElementContentWhitespace(true);
		try{
			DocumentBuilder db=dbf.newDocumentBuilder();
			Document xmldoc=db.parse(xmlPath);
			Element root=xmldoc.getDocumentElement();
			Element son=(Element) selectSingleNode("",root);
			root.removeChild(son);
			
			TransformerFactory factory =TransformerFactory.newInstance();
			Transformer former= factory.newTransformer();
			former.transform(new DOMSource(xmldoc), new StreamResult(new File(xmlPath)));
		}catch(Exception e){
			
		}
	}

	private static Node selectSingleNode(String express, Element source) {
		Node result=null;
		XPathFactory xpathFactory=XPathFactory.newInstance();;
		XPath xpath=xpathFactory.newXPath();
		try {
			result=(Node)xpath.evaluate(express, source, XPathConstants.NODE);
		} catch (XPathExpressionException e) {			
			e.printStackTrace();
		};
		
		return null;
	}
}
